<?php

namespace App\Http\Controllers\Api\V1\Traits;


use App\Models\Coupon;
use App\Models\User;
use Carbon\Carbon;

trait CouponTrait
{

    public function addCoupon($user, $type, $name, $expired_at, $discount = 0)
    {
        if ($user instanceof User) {
            $user = $user->id;
        }

        $res = Coupon::create([
            'user_id' => $user,
            'name' => $name,
            'type' => $type,
            'expired_at' => Carbon::parse($expired_at),
            'discount' => $discount,
        ]);

        return $res;
    }

}